import Vue from 'vue'
import VueRouter from 'vue-router'

//VUE router 导航重复点击报错的问题解决方案
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
//修改原型对象中的push方法，replace方法就把VueRouter.prototype.push的push换成replace即可
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '<manager>',
    component: () => import( '../views/Manager.vue'),
    redirect:'/login',   //重定向到主页
    children:[
      {
        path:'home',
        name:'Home',
        meta:{name:'系统首页'},
        component:()=>import('../views/manager/Home.vue'),
      },
      {
        path:'user',
        name:'User',
        meta:{name:'用户信息'},
        component:()=>import('../views/manager/User.vue'),
      },
      {
        path: 'choose',
        name: 'Choose',
        meta:{name:'师生关系'},
        component: () => import( '../views/manager/Choose.vue')
      },
      {
        path: '/403',
        name: 'Auth',
        meta:{name:'无权限'},
        component: () => import( '../views/manager/Auth.vue')
      },
      {
        path: '/person',
        name: 'Person',
        meta:{name:'个人信息'},
        component: () => import( '../views/manager/Person.vue')
      },
      {
        path: '/password',
        name: 'Password',
        meta:{name:'修改密码'},
        component: () => import( '../views/manager/Password.vue')
      },
      {
        path: '/audit',
        name: 'Audit',
        meta:{name:'项目申报'},
        component: () => import( '../views/manager/Audit.vue')
      },
      {
        path: '/teacher',
        name: 'Teacher',
        meta:{name:'项目审阅'},
        component: () => import( '../views/manager/Teacher.vue')
      },
      {
        path: '/select',
        name: 'Select',
        meta:{name:'选择教师'},
        component: () => import( '../views/manager/SelectTeacher.vue')
      },
      {
        path: '/notice',
        name: 'Notice',
        meta:{name:'系统公告管理'},
        component: () => import( '../views/manager/Notice.vue')
      },
      {
        path: '/shownotice',
        name: 'ShowNotice',
        meta:{name:'系统公告'},
        component: () => import( '../views/manager/ShowNotice.vue')
      },
      {
        path: '/upload',
        name: 'Upload',
        meta:{name:'上传中心'},
        component: () => import( '../views/manager/Upload.vue')
      },
      {
        path: '/download',
        name: 'Download',
        meta:{name:'下载中心'},
        component: () => import( '../views/manager/Download.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    meta:{name:'登录'},
    component: () => import( '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    meta:{name:'注册'},
    component: () => import( '../views/Register.vue')
  },
  {
    path: '*',
    name: '404',
    meta:{name:'无法访问'},
    component: () => import( '../views/404.vue')
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  //to 是到达路由
  //from 是开源的路由信息
  //next 是帮助我们跳转的路由
  let adminPaths =['/user']
  let user =JSON.parse(localStorage.getItem('honey-user') ||'{}')
  if(user.role!=='管理员' && adminPaths.includes(to.path)){  
    //如果当前登录的用户不是管理员，然后到达的路径是管理员才有权限访问的路径，这时候我们需要让用户去一个没有权限的页面403，不让他访问实际的页面
    next('/403')
  }else{
    next()
  }
})

export default router
